<script>
import {storage} from '@/utils';
import UserSelect from '@/components/userselect'
export default {
  components: {UserSelect},
  data() {
    return {
      dialogVisible: false,
      form: {
        name: ""
      },
      query: this.$route.query,
      fileList: [],
      roles: [],
      uploadUrl: $conf.baseURL + "/enclosureInfo/addOne",
      approveNode: [],
      index: "", //选择审批流程数组的下标
      userIndex: "",
      bizType: "",
      status: "application", // application 申请状态  approve 审批状态  appliced //已申请
      approveTask: [], //获取审批状态
      uploadType: "",
      control: false,
      checkList: [],
      isSingle: true,
      form1: {
        desc: ""
      },
      roleType: [],
      roleId: '',
      projectLevel: '',
      rejectVisible: false,
      rejectText: '',
      activeName: 'first'
    };
  },
  methods: {
    changeCheckList(newCheckList){
      this.checkList = newCheckList
    },
    uploadSuccess() {
      this.showSuccessMessage("上传成功");
      this.getUploadFile(this.query.id);
    },
    getUploadFile(id) {
      let params = {
        bizId: id,
        bizType: this.uploadType
      };
      this.$store.dispatch("common/getEnclosureInfo", params).then(res => {
        this.fileList = res;
      });
    },
    uploadError() {
      this.showErrorMessage("上传失败");
    },
    remove(id) {
      let params = {
        id: id
      };
      this.$store.dispatch("common/removeEnclosure", params).then(res => {
        this.showSuccessMessage("删除成功");
        this.getUploadFile(this.query.id);
      });
    },
    getRoles(name) {
      let params = {
        roleId: roleId[0].id
      };
      this.$store.dispatch("project/getRoles", params).then(res => {
        this.roles = res;
      });
    },
    commit() {
      let notifyContent = '';
      let modelId = '';
      let requestParam = null;
      if(this.query.projectStatus == '6') {
        notifyContent = storage.get('loginInfo').userInfoVO.userName + "提交了《" + this.query.name + "》结项审批"; 
        modelId = $conf.modelId['settlement'];
      } else if (this.query.projectStatus == '1') {
        if(!this.projectLevel) {
          return this.$alert('请选择项目等级');
        }
        notifyContent = storage.get('loginInfo').userInfoVO.userName + "提交了《" + this.query.name + "》立项审批"; 
        modelId = $conf.modelId['application'];
        requestParam = {id: this.query.id, projectLevel: this.projectLevel.key};
      }
      //选择了的审批流程
      let selectApproveNode = this.approveNode.map(item => {
        if (item.userList.length === 0) {
          this.$alert("请选择审批人");
          return;
        }
        if (item.approveType == 1) {
          return {
            approveNodeId: item.id,
            userId: item.userList[0].id
          };
        } else {
          return {
            approveNodeId: item.id,
            userIdList: item.userList.map(user => {
              return user.id;
            })
          };
        }
      });

      let params = {
        modelId: modelId, //审批流编号
        bizId: this.query.id, //业务编号
        bizType: this.bizType, //项目状态
        approveNodeList: selectApproveNode, //审批节点设置集合
        notifyContent: notifyContent,
        requestParam: JSON.stringify(requestParam)
      };
      this.$store.dispatch("project/submitApprove", params).then(res => {
        this.showSuccessMessage("提交申请成功");
        this.status = 'appliced';
        this.getApproveNode(modelId);
      });
    },
    getApproveNode(id) {
      let params = {
        modelId: id
      };
      this.$store.dispatch("project/getApproveNode", params).then(res => {
        this.approveNode = res.map(item => {
          item.userList = [];
          return item;
        });
        console.log(this.status);
        if(this.status != 'application') {
          this.getUserName();
        }
      });
    },
    select(name, index, approveType) {
      this.index = index;
      this.checkList = [];
      if(approveType == '2') {
        this.isSingle = false;
      } else {
        this.isSingle = true;
      }
      if(name == '组长' || name == '所长' || name == '院长') {
        name = '普通用户';
      }
      let roleId = this.roleType.filter(item => {
        return item.name == name;
      })
      this.roleId = roleId[0].id;
      this.dialogVisible = true;
      // this.getRoles(name);
    },
    selectedRole() {
      //如果存在UserIndex说明是重新选择
      if (this.userIndex !== "") {
        this.approveNode[this.index].userList[this.userIndex] = JSON.parse(JSON.stringify(this.checkList[0]));
      } else {
        this.approveNode[this.index].userList = JSON.parse(JSON.stringify(this.checkList));
      }
      this.form.name = "";
      this.userIndex = "";
      this.dialogVisible = false;
    },
    reselect(index, userName, userIndex, name, approveType) {
      if(this.status == 'application') {
         this.checkList = [];
        if(approveType == '2') {
          this.isSingle = false;
        } else {
          this.isSingle = true;
        }
        if(name == '组长' || name == '所长' || name == '院长') {
          name = '普通用户';
        }
        let roleId = this.roleType.filter(item => {
          return item.name == name;
        })
        this.roleId = roleId[0].id;
        this.checkList = this.approveNode[this.index].userList
        this.dialogVisible = true;
      }
    },
    getUserName() {
      if(!this.$route.query.id) {
        return;
      }
      this.queryApproveByBiz(this.$route.query.id, this.bizType).then(res => {
        if(!res) {
          return;
        }
        this.approveTask = res;
        this.form1.desc = res.approveRequest.requestDescribe;
        //根据审批编号获取的当前审批的具体数据，并和审批节点匹配
        res.approveTaskList.forEach(item => {
          this.approveNode.forEach(node => {
            //和审批节点进行匹配
            if (item.nodeName == node.name) {
              node.userList = item.userApproveTaskList.map(el => {
                return {
                  name: el.userName,
                  id: el.userId,
                  approveStatus: el.approveStatus,
                  taskId: el.taskId,
                  approveText: $status.approveText[el.approveStatus],
                  remark: el.remark,
                  createTime: el.createTime,
                  remark: res.approveRequest.requestDescribe,
                  approveOpinion: el.approveOpinion
                };
              });
            }
          });
        });
        //判断当前用户有无权限通过或驳回
        this.authority();
      });
    },
    //判断当前登录是否有权限通过或驳回
    authority() {
      let task;
      this.control = false;
      this.approveTask.approveTaskList.forEach(item => {
        item.userApproveTaskList.forEach(el => {
          // 当当前用户的Id和审批节点匹配并审批状态为2时才能进行操作
          if(storage.get('loginInfo').userInfoVO.id == el.userId && el.approveStatus == '2') {
            //如果匹配的话，就返回任务编号
            task = {
              taskId: el.taskId,
              approveStatus: el.approveStatus
            };
            this.control = true;
          }
        })
      })
      return task;
    },
    pass() {
      let task = this.authority();
      if(this.authority()) {
        let params = {
          taskId: task.taskId,
          approveOpinion: '2',
          approveStatus: '2'
        }
        this.$store.dispatch("common/approve", params).then(res => {
          this.showSuccessMessage('审批通过成功');
          this.getUserName();
          this.authority();
        });
      } else {
        this.$alert('对不起，您没权限进行操作')
      }
    },
    reject() {
      let task = this.authority();
      if(this.authority()) {
        let params = {
          taskId: task.taskId,
          approveOpinion: this.rejectText,
          approveStatus: '3'
        }
        this.$store.dispatch('common/approve', params).then(res => {
          this.showSuccessMessage('审批驳回成功');
          this.getUserName();
          this.authority();
          this.rejectVisible = false;
        }); 
      } else {
        this.$alert('对不起，您没权限进行操作')
      }
    }
  },
  mounted() {
    let params = {

    }
    this.$store.dispatch('common/listRoleInfo', params).then(res => {
      this.roleType = res.map(item => {
        return {
          name: item.name,
          id: item.id
        }
      })
    });
  }
};
</script>